Main
2019-06-03

Convert images to PDF with ImageMagic

I often have to join multiple images into one PDF file. My way to do this was always to convert the images to PDF files using ImageMagick and then concatenate the PDF files with pdftk.

Lately this failed with the following error:

convert: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.

The reason is a change in the default policies that forbids to convert files to PDF. The motivation for this is that ImageMagick uses Ghostscript as PDF back end and Ghostscript had several security issues.

You can find the policies files and the policies set by convert -list policy. You could open the policy file and disable the line that states

<policy domain="coder" rights="none" pattern="{PS,PS2,PS3,EPS,PDF,XPS}" />

to be able to write PDF files again. Be aware this would get overwritten on you next ImageMagick-update. Better create a policy file in your home directory. On Linux this is most likely `${HOME}/.config/ImageMagick/policy.xml1. In that you can enable PDF reading and writing support by a simpel policy. The policy file should look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policymap [
  <!ELEMENT policymap (policy)+>
  <!ATTLIST policymap xmlns CDATA #FIXED ''>
  <!ELEMENT policy EMPTY>
  <!ATTLIST policy xmlns CDATA #FIXED '' domain NMTOKEN #REQUIRED
    name NMTOKEN #IMPLIED pattern CDATA #IMPLIED rights NMTOKEN #IMPLIED
    stealth NMTOKEN #IMPLIED value CDATA #IMPLIED>
]>
<policymap>
  <policy domain="coder" rights="read | write" pattern="PDF" />
</policymap>

Now you can again create you pdf files the good old way

$ convert foo.jpg foo.pdf
$ convert bar.png bar.pdf
$ pdftk foo.pdf bar.pdf cat output foobar.pdf

Of course you should think about the security implications for your system.


pdf en


Creative Commons License Convert images to PDF with ImageMagic by Michael F. Schönitzer is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.